package org.iridium.xl.order;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.iridium.xl.order.vo.ProductItem;
import org.iridium.xl.order.vo.VstOrder;

/**
 *
 * @author iridiumcao
 */
public class DataParser {

    public List<VstOrder> buildOrderList(String excelPath) {
        List<VstOrder> orders = new ArrayList<VstOrder>();
        try {
            XSSFWorkbook wb = new XSSFWorkbook(excelPath);
            XSSFSheet sheet = wb.getSheetAt(0);
            int rowNum = sheet.getLastRowNum(); // 得到总行数
            VstOrder order = new VstOrder();
            for (int i = 1; i < rowNum; i++) {
                XSSFRow row = sheet.getRow(i);
                // System.out.println(row.getCell(10).getStringCellValue());
                if (row.getCell(0) != null) {
                    // 提取一个VstOrder对象的数据和一条productItem对象的数据
                    order = new VstOrder();
                    orders.add(order);
                    order.setUserName(row.getCell(0).getStringCellValue());// 用户名
                    order.setCustomNo(row.getCell(1).getRawValue());// 客户编号
                    order.setOrderNo(row.getCell(2).getStringCellValue()); // 订单编号
                    // TODO
                    // 是送货单号吗？
                    order.setOrderDate(row.getCell(3).getDateCellValue()); // 下单时间
                    // TODO
                    // 不是制单日期
                    order.setAcceptor(row.getCell(4).getStringCellValue()); // 收件人
                    order.setAddress(row.getCell(5).getStringCellValue()); // 送货地址(收件地址)
                    order.setTel(row.getCell(6).getRawValue()); // 联系电话

                    buildProductItem(order, row);
                } else if (row.getCell(0) == null) {
                    // 提取一条 productItem 的数据，并加入list
                    buildProductItem(order, row);
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return orders;
    }

    private void buildProductItem(VstOrder order, XSSFRow row) {
        ProductItem p = new ProductItem();
        p.setProductCode(row.getCell(7).getStringCellValue());// 商品代码
        p.setProductName(row.getCell(8).getStringCellValue()); // 商品名称
        p.setClazz(row.getCell(9).getStringCellValue());// 规格
        p.setModel(row.getCell(10).getStringCellValue());// 单位
        p.setCount((int) (row.getCell(11).getNumericCellValue()));// 数量
        p.setPrice(row.getCell(12).getNumericCellValue());// 单价
        p.setAmount(row.getCell(13).getNumericCellValue());// 金额 = count*price
        // p.setMemo(row.getCell(14).getStringCellValue());// 备注

        order.getProducts().add(p);
    }

    public static void main(String... args) {
        DataParser hello = new DataParser();
        String excel = "D:\\EverBox\\others\\打印xl\\商品清单打印.xlsx";
        List<VstOrder> orders = hello.buildOrderList(excel);
        for (VstOrder order : orders) {
            System.out.print("用户名：" + order.getUserName() + ", ");
            System.out.print("客户编号: " + order.getCustomNo() + ", ");
            System.out.print("订单编号: " + order.getOrderNo() + ", ");
            System.out.print("下单时间: " + order.getOrderDate() + ", ");
            System.out.print("收件人: " + order.getAcceptor() + ", ");
            System.out.print("送货地址: " + order.getAddress() + ", ");
            System.out.println("联系电话: " + order.getTel());

            for (ProductItem product : order.getProducts()) {
                System.out.print("\t\t\t商品代码：" + product.getProductCode()
                        + ", ");
                System.out.print("商品名称：" + product.getProductName() + ", ");
                System.out.print("单位：" + product.getModel() + ", ");
                System.out.print("数量：" + product.getCount() + ", ");
                System.out.print("单价：" + product.getPrice() + ", ");
                System.out.println("金额：" + product.getAmount());
            }
            System.out.println();
        }
    }
}